package com.wjr.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wjr.model.Dept;
import com.wjr.service.DeptService;
import com.wjr.service.impl.DeptServiceImpl;

public class DeptServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private DeptService deptService = null;
	private Dept t = null;
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String mark = request.getParameter("mark");
		if("insert".equals(mark)){
			try {
				insert(request,response);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else if("findAll".equals(mark)){
			try {
				findAll(request,response);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else if("delete".equals(mark)){
			try {
				delete(request,response);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else if("findById".equals(mark)){
			try {
				findById(request,response);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else if("toUpdate".equals(mark)){
			try {
				toUpdate(request,response);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else if("update".equals(mark)){
			try {
				update(request,response);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}
	
	/**
	 * 新增部门
	 * @param request
	 * @param response
	 * @throws SQLException
	 * @throws ServletException
	 * @throws IOException
	 */
	private void insert(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException{
		String dno = request.getParameter("dno");
		String dname = request.getParameter("dname");
		String ddesc = request.getParameter("ddesc");
		
		t = new Dept();
		t.setDno(dno);
		t.setDname(dname);
		t.setDdesc(ddesc);
		
		deptService = new DeptServiceImpl();
		boolean flag = deptService.insert(t);
		
		if(flag == false){
			findAll(request, response);
		}else{
			request.getRequestDispatcher("../do_insert_dept,jsp").forward(request, response);	
		}
	}
	
	/**
	 * 查询所有的部门
	 * @param request
	 * @param response
	 * @throws SQLException
	 * @throws ServletException
	 * @throws IOException
	 */
	private void findAll(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException{
		deptService = new DeptServiceImpl();
		List<Dept> list = deptService.findAll();
		request.setAttribute("list",list);
		request.getRequestDispatcher("../deptList.jsp").forward(request, response);	
	}
	
	/**
	 * 删除部门
	 * @param request
	 * @param response
	 * @throws SQLException
	 * @throws ServletException
	 * @throws IOException
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException{
		deptService = new DeptServiceImpl();
		String id = request.getParameter("id");
		deptService.delete(Integer.valueOf(id));
		findAll(request, response);
	}
	
	/**
	 * 根据部门编号查询部门
	 * @param request
	 * @param response
	 * @throws SQLException
	 * @throws ServletException
	 * @throws IOException
	 */
	private void findById(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException{
		deptService = new DeptServiceImpl();
		String no = request.getParameter("dno");
		List<Dept> list = deptService.findById(no);
		request.setAttribute("list",list);
		request.getRequestDispatcher("../deptList.jsp").forward(request, response);	
	}
	
	/**
	 * 跳转到更新部门页面
	 * @param request
	 * @param response
	 * @throws SQLException
	 * @throws ServletException
	 * @throws IOException
	 */
	private void toUpdate(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException{
		deptService = new DeptServiceImpl();
		
		String no = request.getParameter("dno");
		List<Dept> list = deptService.findById(no);
		request.setAttribute("list",list);
		request.getRequestDispatcher("../do_update_dept.jsp").forward(request, response);	
	}
	
	/**
	 * 更新部门
	 * @param request
	 * @param response
	 * @throws SQLException
	 * @throws ServletException
	 * @throws IOException
	 */
	private void update(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException{
		deptService = new DeptServiceImpl();
		int id = Integer.valueOf(request.getParameter("id"));
		String dno = request.getParameter("dno");
		String dname = request.getParameter("dname");
		String ddesc = request.getParameter("ddesc");
		
		t = new Dept();
		t.setId(id);
		t.setDno(dno);
		t.setDname(dname);
		t.setDdesc(ddesc);
		
		boolean flag = deptService.update(t);
		if(flag==false){
			findAll(request, response);
		}else{
			request.getRequestDispatcher("../do_update_dept.jsp").forward(request, response);		
		}
	}

}
